/**
 * 创建三维地球
 * @param {dom} box 创建三维地球的dom容器
 * @param {string} options.tdtKey 天地图Key
 * 
 * @returns {Earth} earth实例
 */
function EarthInit(box, options) {
    // 1.1.1 创建地球
    let earth = new XE.Earth(box);
    earth.clickNothing = null
    earth.wheelList = []
    let tdtKey = options.tdtKey || 'ab8ff1ae1ca029103e4dc130b366ce26'

    // 获取 Cesium 的 viewer对象  或  通过earth._对象 获取Cesium对象
    var viewer = earth.czm.viewer;
    // 鼠标中键修改为地图缩放效果
    viewer.scene.screenSpaceCameraController.zoomEventTypes = [Cesium.CameraEventType.WHEEL, Cesium.CameraEventType.PINCH];
    // 鼠标右键修改为地图视角旋转效果
    viewer.scene.screenSpaceCameraController.tiltEventTypes = [Cesium.CameraEventType.PINCH, Cesium.CameraEventType.RIGHT_DRAG];
    // 删除鼠标双击事件
    viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);


    // 限制缩放高度
    viewer.scene.screenSpaceCameraController.minimumZoomDistance = 10;
    // viewer.scene.screenSpaceCameraController.maximumZoomDistance = 4000000;

    // 隐藏地球
    viewer.scene.sun.show = false; //在Cesium1.6(不确定)之后的版本会显示太阳和月亮，不关闭会影响展示
    viewer.scene.moon.show = false;
    viewer.scene.skyBox.show = false;//关闭天空盒，否则会显示天空颜色

    // viewer.scene.undergroundMode = true; //重要，开启地下模式，设置基色透明，这样就看不见黑色地球了
    // viewer.scene.underGlobe.show = true;
    // viewer.scene.underGlobe.baseColor = new Cesium.Color(0, 0, 0, 0);
    viewer.scene.globe.show = false; //不显示地球，这条和地球透明度选一个就可以
    viewer.scene.globe.baseColor = new Cesium.Color(0, 0, 0, 0);
    viewer.scene.backgroundcolor = new Cesium.Color(0, 0, 0, 0);
    // 隐藏地球结束


    // js的Date时间格式转换成儒略时间
    let time = '2023-08-10 12:46:06.000'
    time = new Date(time) //Thu Aug 10 2023 16:46:06 GMT+0800 (中国标准时间)
    time = Cesium.JulianDate.fromDate(time) //JulianDate{dayNumber:**, secondsOfDay:**}
    // 设置当前时间
    viewer.clock.currentTime = time;

    // viewer.scene.mode === Cesium.SceneMode.COLUMBUS_VIEW


    // 服务域名
    const tdtUrl = 'http://t{s}.tianditu.gov.cn/';
    // 服务负载子域
    const subdomains = ['0', '1', '2', '3', '4', '5', '6', '7'];


    // 叠加影像服务
    let imgMap = new Cesium.UrlTemplateImageryProvider({
        url: tdtUrl + 'img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=' + tdtKey,
        subdomains: subdomains,
        tilingScheme: new Cesium.WebMercatorTilingScheme(),
        maximumLevel: 18
    });
    // viewer.imageryLayers.addImageryProvider(imgMap); 



    // 叠加标注服务
    let cvaMap = new Cesium.UrlTemplateImageryProvider({
        url: tdtUrl + 'DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=' + tdtKey,
        subdomains: subdomains,
        tilingScheme: new Cesium.WebMercatorTilingScheme(),
        maximumLevel: 18
    });
    // viewer.imageryLayers.addImageryProvider(cvaMap); 

    let canvasbox = earth.czm.viewer.canvas

    // 设置投影
    earth.effect.shadow.enabled = false


    // 防止长按拖拽触发click
    let mouseClick = false
    const handler = new Cesium.ScreenSpaceEventHandler(canvasbox);
    handler.setInputAction(function (e) {
        mouseClick = true
        setTimeout(() => {
            mouseClick = false
        }, 200)
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN)

    handler.setInputAction(function (e) {
        if (!mouseClick) {
            return
        }
        // 通过平面坐标拾取对应模型
        let picked = earth.czm.viewer.scene.pick(e.position);

        console.log(picked)

        // 判断是否拾取到模型
        if (!picked) {
            if (earth.clickNothing) {
                // 点击地图空白执行方法
                earth.clickNothing()
            }
            return
        }
        // 执行被点击模型的click事件 _node为对象的Darw类型
        if (picked.id && picked.id._node) {
            picked.id._node.click()
        }

    }, Cesium.ScreenSpaceEventType.LEFT_UP)

    handler.setInputAction(function (e) {
        if (earth.wheelList) {
            earth.wheelList.forEach(item => { item() })
        }
    }, Cesium.ScreenSpaceEventType.WHEEL)

    return earth;
}


export default EarthInit